package io.renren.modules.efinance.controller;

import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.common.utils.ShiroUtils;
import io.renren.modules.efinance.entity.BankcardEntity;
import io.renren.modules.efinance.service.BankcardService;
import io.renren.modules.efinance.vo.BindCardVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Date;
import java.util.Map;



/**
 * 
 *
 * @author lhf
 * @email i@lhf.com
 * @date 2022-06-24 17:18:43
 */
@RestController
@RequestMapping("efinance/bankcard")
public class BankcardController {
    @Autowired
    private BankcardService bankcardService;

    /**
     * 列表，当前用户的所有银行卡（包括未绑定的）
     */
    @RequestMapping("/list")
    // @RequiresPermissions("efinance:bankcard:list")
    public R list(@RequestParam Map<String, Object> params){
        params.put("bankcard_username",ShiroUtils.getUserEntity().getUsername());
        PageUtils page = bankcardService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 当前用户的所有未绑定的银行卡
     * @param params
     * @return
     */
    @RequestMapping("/list-unbind")
    // @RequiresPermissions("efinance:bankcard:list")
    public R listUnbind(@RequestParam Map<String, Object> params){
        params.put("bankcard_username",ShiroUtils.getUserEntity().getUsername());
        PageUtils page = bankcardService.queryUnbinds(params);

        return R.ok().put("page", page);
    }

    /**
     * 当前用户的所有绑定的银行卡
     * @param params
     * @return
     */
    @RequestMapping("/list-bind")
    // @RequiresPermissions("efinance:bankcard:list")
    public R listBind(@RequestParam Map<String, Object> params){
        params.put("bankcard_username",ShiroUtils.getUserEntity().getUsername());
        PageUtils page = bankcardService.queryBinds(params);

        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{bankcardId}")
    // @RequiresPermissions("efinance:bankcard:info")
    public R info(@PathVariable("bankcardId") String bankcardId){
		BankcardEntity bankcard = bankcardService.getById(bankcardId);

        return R.ok().put("bankcard", bankcard);
    }

    /**
     * 保存,即绑卡，用银行卡号和账户名绑定
     */
    @RequestMapping("/save")
    // @RequiresPermissions("efinance:bankcard:save")
    public R save(@RequestBody BankcardEntity bankcard){
        populateField(bankcard);
		bankcardService.save(bankcard);

        return R.ok();
    }

    private void populateField(BankcardEntity bankcard) {
        bankcard.setBankcardBalance(new BigDecimal(100));
        bankcard.setBankcardOpentime(new Date());
        bankcard.setBankcardUsername(ShiroUtils.getUserEntity().getUsername());
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    // @RequiresPermissions("efinance:bankcard:update")
    public R update(@RequestBody BankcardEntity bankcard){
		bankcardService.updateById(bankcard);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    // @RequiresPermissions("efinance:bankcard:delete")
    public R delete(@RequestBody String[] bankcardIds){
		bankcardService.removeByIds(Arrays.asList(bankcardIds));

        return R.ok();
    }

}
